﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.StandardCustomerFroms
{
    public partial class StandardCustomerAddForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.GoodBll goodsBll = new BLL.GoodBll();
        BLL.TestItemBll testItemBll = new BLL.TestItemBll();
        BLL.CustomerBll customerBll = new BLL.CustomerBll();
        BLL.StandardCustomerBll standardCustomerBll = new BLL.StandardCustomerBll();
        BLL.ToolBll toolBll = new BLL.ToolBll();
        Action _InitGrid;
        StandardCustomer _standardCustomer;
        int _option;
        public StandardCustomerAddForm(Action InitGrid)
        {
            InitializeComponent();
            _option = 1;
            _InitGrid = InitGrid;
        }

        public StandardCustomerAddForm(StandardCustomer standardCustomer, Action InitGrid)
        {
            InitializeComponent();
            _option = 2;
            _standardCustomer = standardCustomer;
            _InitGrid = InitGrid;
        }

        private void StandardCustomerAddForm_Load(object sender, EventArgs e)
        {
            if (_option == 1)
            {
                //初始化样品名称下拉框
                var goodsModels = toolBll.GetSemiGoodList();
                var testModels = testItemBll.LoadEntities(m => true).ToList();
                var customerModels = customerBll.LoadEntities(m => true).ToList();
                this.searchLookUpEdit_goodsName.Properties.DataSource = goodsModels;
                this.searchLookUpEdit_TestItemName.Properties.DataSource = testModels;
                this.searchLookUpEdit_Customer.Properties.DataSource = customerModels;
            }
            if (_option == 2)
            {
                this.searchLookUpEdit_goodsName.Properties.DataSource = toolBll.GetAllGoodList();
                this.searchLookUpEdit_TestItemName.Properties.DataSource = toolBll.GetAllTestItemList();
                this.searchLookUpEdit_Customer.Properties.DataSource = toolBll.GetAllCustomerList();
                this.searchLookUpEdit_Customer.Enabled = false;
                this.searchLookUpEdit_goodsName.Enabled = false;
                this.searchLookUpEdit_TestItemName.Enabled = false;

                this.searchLookUpEdit_goodsName.EditValue = _standardCustomer.GoodsID;
                this.searchLookUpEdit_TestItemName.EditValue = _standardCustomer.TestItemID;
                this.searchLookUpEdit_Customer.EditValue = _standardCustomer.CustomerID;
                this.textEdit_MaxValue.Text = _standardCustomer.MaxValue.ToString();
                this.textEdit_minValue.Text = _standardCustomer.MinValue.ToString();
                this.textEdit_Order.Text = _standardCustomer.FormOrder.ToString();
                this.textEdit_Remark.Text = _standardCustomer.Remark;
            }


        }

        private void simpleButton_save_Click(object sender, EventArgs e)
        {
            if (this.searchLookUpEdit_Customer.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                this.searchLookUpEdit_Customer.Focus();
                return;
            }
            if (this.searchLookUpEdit_goodsName.EditValue == null || searchLookUpEdit_goodsName.EditValue.ToString() == "")
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                this.searchLookUpEdit_goodsName.Focus();
                return;
            }
            if (this.searchLookUpEdit_TestItemName.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                this.searchLookUpEdit_TestItemName.Focus();
                return;
            }
            if (this.textEdit_Order.Text == "")
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                textEdit_Order.Focus();
                return;
            }


            if (_option == 1)
            {
                StandardCustomer standardCustomer = new StandardCustomer();
                standardCustomer.StandardCustomerID = Guid.NewGuid().ToString();
                standardCustomer.CustomerID = Convert.ToString(this.searchLookUpEdit_Customer.EditValue);
                standardCustomer.GoodsID = Convert.ToString(this.searchLookUpEdit_goodsName.EditValue);
                standardCustomer.TestItemID = Convert.ToString(this.searchLookUpEdit_TestItemName.EditValue);
                try
                {
                    standardCustomer.FormOrder = Convert.ToInt32(this.textEdit_Order.Text);
                    if (textEdit_MaxValue.Text != "")
                    {
                        Convert.ToDouble(this.textEdit_MaxValue.Text);
                        standardCustomer.MaxValue = this.textEdit_MaxValue.Text;
                    }
                    else
                    {
                        standardCustomer.MaxValue = null;
                    }
                    if (textEdit_minValue.Text != "")
                    {
                        Convert.ToDouble(this.textEdit_minValue.Text);
                        standardCustomer.MinValue = this.textEdit_minValue.Text;
                    }
                    else
                    {
                        standardCustomer.MinValue = null;
                    }

                    standardCustomer.Remark = this.textEdit_Remark.Text;

                    if (standardCustomer.MinValue != null && standardCustomer.MaxValue != null)
                    {

                        if (Convert.ToDouble(standardCustomer.MinValue) > Convert.ToDouble(standardCustomer.MaxValue))
                        {
                            XtraMessageBox.Show(ResourceOverall.ValueCompareErr);
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message);
                    return;
                }
                //判断重复
                using (OJCB4Entities db = new OJCB4Entities())
                {
                    int count = db.StandardCustomers.Where(m => m.GoodsID == standardCustomer.GoodsID && m.TestItemID == standardCustomer.TestItemID && m.CustomerID == standardCustomer.CustomerID).Count();
                    if (count>0)
                    {
                        XtraMessageBox.Show(ResourceOverall.DataExists);
                        return;
                    }
                    db.StandardCustomers.Add(standardCustomer);
                    var res = db.SaveChanges() > 0;
                    if (res)
                    {
                        _InitGrid();
                        XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                    }
                }


            }
            if (_option == 2)
            {
                try
                {
                    //_standardCustomer.CustomerID = Convert.ToInt32(this.searchLookUpEdit_Customer.EditValue);
                    //_standardCustomer.GoodsID = Convert.ToInt32(this.searchLookUpEdit_goodsName.EditValue);
                    //_standardCustomer.TestItemID = Convert.ToInt32(this.searchLookUpEdit_TestItemName.EditValue);
                    using (OJCB4Entities db = new OJCB4Entities())
                    {
                        StandardCustomer standard = db.StandardCustomers.Find(_standardCustomer.StandardCustomerID);
                        standard.FormOrder = Convert.ToInt32(this.textEdit_Order.Text);
                        if (textEdit_MaxValue.Text != "")
                        {
                            Convert.ToDouble(this.textEdit_MaxValue.Text);
                            standard.MaxValue = this.textEdit_MaxValue.Text;
                        }
                        else
                        {
                            standard.MaxValue = null;
                        }
                        if (textEdit_minValue.Text != "")
                        {
                            Convert.ToDouble(this.textEdit_minValue.Text);
                            standard.MinValue = this.textEdit_minValue.Text;
                        }
                        else
                        {
                            standard.MinValue = null;
                        }
                        standard.Remark = this.textEdit_Remark.Text;
                        var res = db.SaveChanges()>0;
                        if (res)
                        {
                            XtraMessageBox.Show(ResourceOverall.EditSucceed);
                        }
                    }

                }
                catch (Exception ex)
                {
                    _InitGrid();
                    XtraMessageBox.Show(ex.Message);
                    return;
                }

            }
        }
    }
}